<?php 
session_start();
class Add_manager extends CI_Controller{
    public function __construct() {
    parent::__construct();
    $this->load->model('Account_m');
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->library('session');    
    }
    public function index()
    {                
        $password=$this->input->post('password');
        $name=$this->input->post('name');
        $id_mg=$this->input->post('id');
        $email=$this->input->post('email');
        $adress=$this->input->post('adress');
        $phone=$this->input->post('phone');
        $company=$this->input->post('company');
        $year=$this->input->post('year');
        /////Check time
         $pieces = explode(" ",$year); 
         $pieces_start_string=explode("/",$pieces[0]); 
         $pieces_end_string=explode("/",$pieces[1]); 
         $pieces_start[0]=(int)$pieces_start_string[0];
         $pieces_start[1]=(int)$pieces_start_string[1];
         $pieces_start[2]=(int)$pieces_start_string[2];
         $pieces_end[0]=(int)$pieces_end_string[0];
         $pieces_end[1]=(int)$pieces_end_string[1];
         $pieces_end[2]=(int)$pieces_end_string[2];
         //echo  $pieces_start[0]."-".$pieces_start[1]."-".$pieces_start[2]."<br>";
         //echo  $pieces_end[0]."-".$pieces_end[1]."-".$pieces_end[2]."<br>";
         $result_check_date_time=$this->check_date_time($pieces_start[0],$pieces_start[1],$pieces_start[2],$pieces_end[0],$pieces_end[1],$pieces_end[2]);
         //echo $result_check_date_time;
        /////
        $session_data_sm = $this->session->userdata('logged_in_ad');
	$id_ad = $session_data_sm['id_ad'];       
        $this->form_validation->set_rules('email', 'email', 'trim|required|xss_clean');
        $this->form_validation->set_rules('id_mg', 'id_mg', 'trim|required|xss_clean');
        if ($result_check_date_time==0)
        {
            echo "<script type='text/javascript'>";	
            echo "alert ('契約の時間が間違います');";
            echo "</script>";
            redirect('admin/add_manager','refresh'); 
        }
        else if($this->check_space($id_mg,$password)==1)
        {
            if( $this->Account_m->add_account_mg($id_mg,'manager')==0)
                {        
                    echo "<script type='text/javascript'>";	
                    echo "alert ('メールのアドレスまたはIDが存在しました!再入力してお願い致します');";
                    echo "</script>";
                    redirect('admin/add_manager','refresh');           
                }
             else {
                        $this->Account_m->inser_database_mg($id_mg,$password,$email,$adress,$id_ad,$phone,$name,'manager',$year);         
                        // COde tao payment
                        //echo "DKM-ID la: ".$id_choice."<br>";
                        //$pieces = explode(" ",$year); 
                        //$this->Account_m->create_payment($id_mg,$pieces[0],$pieces[1],$id_ad,$adress,$phone);
                        //
                        echo "<script type='text/javascript'>";	
                        echo "alert ('団体管理者の作成が成功');";
                        echo "</script>";
                        redirect('admin','refresh'); 
                    }
        }
        else
        {
            redirect('admin/add_manager','refresh');
        }     
    }
   public function check_space($a,$b){
       if($a!=NULL && $b!=NULL)
        {
            return 1;
        }
        else return 0;
        }
    //public function check_date_time($pieces_start[0],$pieces_start[1],$pieces_start[2],$pieces_end[0],$pieces_end[1],$pieces_end[2])
    public function check_date_time($pieces1,$pieces2,$pieces3,$pieces4,$pieces5,$pieces6)
    {
        $result_check=1;
        if (($pieces1<=0)||($pieces2<=0)||($pieces3<=0)||($pieces4<=0)||($pieces5<=0)||($pieces6<=0)) $result_check=0;
        else
        {
            if (($pieces2>=13)||($pieces5>=13)) $result_check=0;
            if (($pieces3>=32)||($pieces6>=32)) $result_check=0;
            if ($pieces1>$pieces4) $result_check=0;
            else
            {
                if($pieces1==$pieces4)
                {
                    if ($pieces2>$pieces5) $result_check=0;
                    else
                    {
                        if(($pieces2==$pieces5))
                        {
                            if($pieces3>$pieces6) $result_check=0;
                        }
                    }
                }
                
            }
        }
        return $result_check;
    }
 }
?>
